<include file="common/header_dc" title="修改密码" body=""/>
<div class="page">
    <div class="page-hd">
        <div class="header">
            <div class="header-left">
                <a href="javascript:history.go(-1)" class="left-arrow"></a>
            </div>
            <div class="header-title">修改密码</div>
            <div class="header-right">
                <a href="#"></a>
            </div>
        </div>
    </div>

    <div class="page-bd">
        <!-- 页面内容-->
        <form method="post" id="payform">
            <div class="weui-cells weui-cells_form">
                <div class="weui-cell">
                    <div class="weui-cell__hd">
                        <label class="weui-label">旧密码</label>
                    </div>
                    <div class="weui-cell__bd">
                        <input class="weui-input" type="password" id="old_password" name="old_password" placeholder="请输入旧密码">
                    </div>
                </div>
                <div class="weui-cell">
                    <div class="weui-cell__hd">
                        <label class="weui-label">新密码</label>
                    </div>
                    <div class="weui-cell__bd">
                        <input class="weui-input" type="password" id="new_password" name="new_password" placeholder="请输入新密码">
                    </div>
                </div>
                <div class="weui-cell">
                    <div class="weui-cell__hd">
                        <label class="weui-label">确认新密码</label>
                    </div>
                    <div class="weui-cell__bd">
                        <input class="weui-input" type="password" id="confirm_password" name="confirm_password" placeholder="请输入新密码">
                    </div>
                </div>
                <div class="weui-cell">
                    <div class="weui-cell__hd">
                        <label class="weui-label">邮箱</label>
                    </div>
                    <div class="weui-cell__bd">
                        <input class="weui-input phone_input" placeholder="请输入邮箱" name="email" id="email" type="text">
                    </div>
                </div>
                <div class="weui-cell">
                    <div class="weui-cell__hd">
                        <label class="weui-label">验证码</label>
                    </div>
                    <div class="weui-cell__bd">
                        <input class="weui-input phone_input" placeholder="请输入验证码" name="email_code" id="email_code"  type="text" style="width: 50%">
                        <a href="javascript:void(0)" id="send" onclick="sendCode()" style="background: #F00;color: #fff;padding: 6px;border-radius: 20px;">获取验证码</a>
                    </div>
                </div>
            </div>
            <div class="weui-btn-area">
                <a href="javascript:void(0);" style="background-image: linear-gradient(250deg, #e62e2e 0%, #e62e2e 100%), linear-gradient(#e62e2e, #e62e2e) !important;box-shadow: 0.033333rem 0.058rem 0.133333rem 0 rgba(157, 157, 157, 0.3);border-radius: 42px;font-size: 0.2rem;height: 0.6rem;line-height: 0.6rem;border: solid 0.006667rem #e62e2e;" class="weui-btn weui-btn_primary" onclick="submits()">确定</a>
            </div>
        </form>
    </div>
</div>
<script src="__PUBLIC__/assets_reg/js/jquery.cookie.js"></script>
<script>
    $(function() {
        FastClick.attach(document.body);

        var obj1 = $("#send");
        v = getCookieValue("secondsremained");//获取cookie值
        if(v>0){
            settime(obj1);//开始倒计时
        }
    });
</script>
<script>
 //提交前验证表单
    function submits() {
        var new_password = $('#new_password').val();
        var confirm_password = $('#confirm_password').val();
        var old_password = $('#old_password').val();
        var email = $('#email').val();
        var emialcode = $('#email_code').val();

        if (new_password == '' || confirm_password== '') {
            showErrorMsg('登录密码不能为空');
            return false;
        }
        if (old_password == '' ) {
            showErrorMsg('原密码不能为空');
            return false;
        }
        if (new_password.length < 6 ) {
            showErrorMsg('密码长度不符合');
            return false;
        }
        if (new_password != confirm_password) {
            showErrorMsg('两次密码不一致');
            return false;
        }
        if (email == '' || email== '') {
            showErrorMsg('邮箱不能为空');
            return false;
        }
        if (emialcode == '' || emialcode== '') {
            showErrorMsg('验证码不能为空');
            return false;
        }
        $.ajax({
            url: "{:U('Member/edit_password')}",
            type: 'post',
            dataType: 'json',
            data: $('#payform').serialize(),
            success: function (data) {
                if (data.code == 0) {
                    $.toast(data.msg, 2000, 'cancel');
                    window.location.href = "{:U('Member/userData')}";
                } else {
                    showErrorMsg(data.msg);
                    return false;
                }
            }
        })
    }

     //发送验证码时添加cookie
     function addCookie(name,value,expiresHours){
         //判断是否设置过期时间,0代表关闭浏览器时失效
         if(expiresHours>0){
             var date=new Date();
             date.setTime(date.getTime()+expiresHours*1000);
             $.cookie(name, escape(value), {expires: date});
         }else{
             $.cookie(name, escape(value));
         }
     }
     //修改cookie的值
     function editCookie(name,value,expiresHours){
         if(expiresHours>0){
             var date=new Date();
             date.setTime(date.getTime()+expiresHours*1000); //单位是毫秒
             $.cookie(name, escape(value), {expires: date});
         } else{
             $.cookie(name, escape(value));
         }
     }
     //根据名字获取cookie的值
     function getCookieValue(name){
         return $.cookie(name);
     }
     //定义倒计时变量
     var countdown;

     // 倒计时函数
     function settime(obj) { //发送验证码倒计时
         countdown=getCookieValue("secondsremained");
         if (countdown === "0") {
             obj.attr('disabled',false);
             //obj.removeattr("disabled");
             obj.text("获取验证码");
             obj.css("background","#f00");
             return;
         } else {
             obj.attr('disabled',true);
             obj.text("重新发送(" + countdown + ")");
             obj.css("background","#999");
             countdown--;
             editCookie("secondsremained",countdown,countdown+2);
         }
         setTimeout(function() {
                 settime(obj) }
             ,1000)
     }

     function sendCode() {
         var mail = $("input[name='email']").val();
         var myreg = /^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
         if(!myreg.test(mail)){
             $.toast("请输入正确的邮箱！", "cancel");
             return false;
         }
         v = getCookieValue("secondsremained");//获取cookie值
         if (v == null || v < 0) {
             var obj = $("#send");
             addCookie("secondsremained", 60, 60);//添加cookie记录,有效时间60s
             settime(obj);//开始倒计时
             $.ajax({
                 method: 'POST',
                 dataType: 'JSON',
                 data: {
                     "email" : mail
                 },
                 headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                 url: "{:U('Login/sendEmailCode')}",
                 success: function (data) {
                     $.toast(data.msg, "success");
                 },error: function (result) {
                     $.toast('请求错误', "cancel");

                 }
             });
         }
     }
    /**
     * 提示弹窗
     * */
    function showErrorMsg(msg) {
        $.toast(msg,'cancel');
    }
    $(function () {
        FastClick.attach(document.body);
    });
</script>
</body>
</html>
